﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;
using System.IO;
using System.Text;
using System.Net.Mail;
using Sherwood.SignOn.Server.Models.Repositories;
using Sherwood.Mvc.Localization;
using Sherwood.SignOn.Server.Content;

namespace Sherwood.SignOn.Server.Models.Services
{
    /// <summary>
    /// Functions to facilitate sending email from the application.
    /// </summary>
    public class EmailHelper
    {
        /// <summary>
        /// Sends an email to the specified user based on the specified template file.
        /// Macro parameters [ParamName] are replaced with values passed in with the replacementDictionary.
        /// Language strings [!String!] are automatically translated using the current translation provider.
        /// </summary>
        /// <param name="to">UserAccount to send email to</param>
        /// <param name="templateFilename">Path to the file to be used as a template. This would typically be
        /// a HTML file containing parameters to be replaced.</param>
        /// <param name="replacementDictionary">Dictionary of parameter keys and corrosponding values which should be replaced in the template.</param>
        /// <returns>True if no errors are encountered</returns>
        public static bool SendEmailToUser(string subject, string emailText, IUserAccount to, string emailAddress)
        {
            string toEmail = emailAddress;
            string toName = !string.IsNullOrEmpty(to.DisplayName) ? to.DisplayName : toEmail;
            if (toEmail != "")
            {
                MailAddress toAddress = new MailAddress(toEmail, toName);
                SmtpClient emailClient = new SmtpClient();
                MailMessage message = new MailMessage();
                message.From = new MailAddress(Config.Settings.SupportEmailAddress, Config.Settings.ProductName);
                message.Sender = new MailAddress(Config.Settings.SupportEmailAddress, Config.Settings.ProductName);
                message.To.Add(toAddress);
                message.IsBodyHtml = false;
                message.Subject = subject;
                message.Body = emailText;
                emailClient.Send(message);
                return true;     
            }
            return false;
        }

       
    }
}
